/*
 * frmMozo.java
 *
 * Created on 12 de junio de 2008, 10:23 AM
 */

package GUI;
import javax.swing.*;
import javax.swing.table.*;
import AccesoDatos.*;
import ReglasNegocio.*;
import ModelosDatos.*;
import org.jdesktop.swingx.autocomplete.*;
/**
 *
 * @author  Administrador
 */
public class frmMozo extends javax.swing.JDialog {

    /** Creates new form frmMozo */
    public frmMozo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboFincas.setModel(this.modeloFincas());
        AutoCompleteDecorator.decorate(comboFincas);
        this.verMozos();
    }
    
     public frmMozo() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboFincas.setModel(this.modeloFincas());
        AutoCompleteDecorator.decorate(comboFincas);
        this.verMozos();
    }
    
    private void MostrarMenu(java.awt.event.MouseEvent e) {
        if(this.PopupMenu.isPopupTrigger(e))
            this.PopupMenu.show(e.getComponent(), e.getX(), e.getY());
    }
    
    private void verMozos() {
        this.tablaMozos.setModel(this.modeloMozos());
        TableColumn col=this.tablaMozos.getColumnModel().getColumn(3);
        JComboBox fincas=new JComboBox(this.modeloFincas());
        col.setCellEditor(new DefaultCellEditor(fincas));
    }
    
    private DatosMozo modeloMozos() {
        DatosMozo modelo=new DatosMozo();
        modelo.addColumn("Id");
        modelo.addColumn("Nombres");
        modelo.addColumn("Apellidos");
        modelo.addColumn("Finca");
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Finca f=(Finca)this.comboFincas.getSelectedItem();
        String buscar="select * from mozo where fkfinca='"+f.getId()+"'";
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long idm=(Long)v.getRegistro(i, 0);
                Dato id=new Dato("id",String.valueOf(idm));
                Mozo m=new Mozo(id);
                String[] fila={m.getId(),m.getNombres(),m.getApellidos(),m.getFinca().getNombre()};
                modelo.addRow(fila);
            }
        }
        return modelo;
    }
    private DefaultComboBoxModel modeloFincas() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        ConMySQL con=new ConMySQL();
        con.Conectar();
        String buscar="select * from finca";
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long id=(Long) v.getRegistro(i, 0);
                Dato idf=new Dato("id",String.valueOf(id));
                Finca f=new Finca(idf);
                modelo.addElement(f);
            }
        }
        con.Cerrar();
        return modelo;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PopupMenu = new javax.swing.JPopupMenu();
        ItemCorte = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtApellidos = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        comboFincas = new javax.swing.JComboBox();
        btnRegistrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaMozos = new javax.swing.JTable();

        ItemCorte.setMnemonic('C');
        ItemCorte.setText("Registrar Corte");
        ItemCorte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ItemCorteActionPerformed(evt);
            }
        });
        PopupMenu.add(ItemCorte);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registrar Mozo"));

        jLabel1.setText("Nombres");

        jLabel2.setText("Apellidos");

        jLabel3.setText("Finca");

        comboFincas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboFincasActionPerformed(evt);
            }
        });

        btnRegistrar.setMnemonic('R');
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRegistrar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboFincas, 0, 321, Short.MAX_VALUE)
                            .addComponent(txtApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                            .addComponent(txtNombres, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboFincas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(btnRegistrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tablaMozos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaMozos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tablaMozosMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tablaMozos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void comboFincasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboFincasActionPerformed
// TODO add your handling code here:
    this.verMozos();
}//GEN-LAST:event_comboFincasActionPerformed

private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
// TODO add your handling code here:
    Finca f=(Finca)this.comboFincas.getSelectedItem();
    Mozo nuevo=new Mozo();
    nuevo.setNombres(this.txtNombres.getText());
    nuevo.setApellidos(this.txtApellidos.getText());
    nuevo.setFinca(f);
    nuevo.Crear();
    this.txtNombres.setText("");
    this.txtApellidos.setText("");
    this.verMozos();
}//GEN-LAST:event_btnRegistrarActionPerformed

private void tablaMozosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMozosMouseReleased
// TODO add your handling code here:
    this.MostrarMenu(evt);
}//GEN-LAST:event_tablaMozosMouseReleased

private void ItemCorteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItemCorteActionPerformed
// TODO add your handling code here:
    try {
        int fila=this.tablaMozos.getSelectedRow();
        String id=(String)this.tablaMozos.getModel().getValueAt(fila, 0);
        Mozo sel=new Mozo(new Dato("id",id));
        String cantidad=JOptionPane.showInputDialog("Introduzca la cantidad de arrobas");
        double arrobas=Double.valueOf(cantidad);
        Corte c=new Corte();
        c.setArroba(arrobas);
        c.setFinca(sel.getFinca());
        c.setMozo(sel);
        c.Crear();
    }
    catch (Exception ex) {}
}//GEN-LAST:event_ItemCorteActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmMozo dialog = new frmMozo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ItemCorte;
    private javax.swing.JPopupMenu PopupMenu;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JComboBox comboFincas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaMozos;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtNombres;
    // End of variables declaration//GEN-END:variables

}
